<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="合同款项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractPayment">
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.contractPayment" dictCode="contract_payment" placeholder="请选择合同款项" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款比例（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ratioPayment" class="ratio_payment">
              <a-input
                style="width: 100%"
                type="number"
                :disabled="formDisabled"
                addon-after="%"
                placeholder="请输入付款比例"
                @blur="inputTaxRateBlur"
                v-model="model.ratioPayment" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentAmount">
              <a-input-number
                :disabled="formDisabled"
                :max="99999999"
                :min="0"
                v-model="model.paymentAmount"
                placeholder="请输入付款金额（元）"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled="formDisabled" :max-length="500" v-model="model.remark" rows="4" placeholder="请输入备注"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  export default {
    name: 'ErpPurchaseContractPaymentForm',
    components: {
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        flag: false,
        ratioPaymentNum: 100,
        model: {
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          contractPayment: [
              { required: true, message: '请选择合同款项!', trigger: 'change' }
          ],
          ratioPayment: [
              { required: true, message: '请输入付款比例!', trigger: 'blur' },
              { required: true, pattern: /^(0(\.\d{1,2})?|[1-9]\d*(\.\d{1,2})?)$/, message: '请输入正确的比例（保留两位小数）!' }
          ],
          paymentAmount: [
              { required: true, message: '请输入付款金额!', trigger: 'blur' },
              { required: true, pattern: /^(0(\.\d{1,2})?|[1-9]\d*(\.\d{1,2})?)$/, message: '付款金额只可保留两位小数!' }
          ]
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs['form'].clearValidate();
        this.model = Object.assign({}, record);
      },

      // 获取付款比例
      calculateRatioPayment (value) {
        this.$nextTick(() => {
          if (value) {
            this.ratioPaymentNum = (100 - Number(value)) // 计算还剩余多少比例
            if (this.ratioPaymentNum) {
              this.ratioPaymentNum = this.ratioPaymentNum.toFixed(2)
              this.ratioPaymentNum = Number(this.ratioPaymentNum)
            }
          } else {
            this.ratioPaymentNum = 100
          }
        })
      },

      // 比例输入框变化时
      inputTaxRateBlur (value) {
        // 去除0(.replace(/\b(0+)/gi, ''))
        var num = value.srcElement._value.toString()
        if (num >= 0 && num <= this.ratioPaymentNum) {
          this.model.ratioPayment = num
        }
        if (num < 0) {
          this.model.ratioPayment = 0
        }
        if (num > this.ratioPaymentNum) {
          this.model.ratioPayment = this.ratioPaymentNum
          this.$message.warning('付款信息列表中付款总比例不能超过100%！')
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          const that = this;
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.$emit('ok', this.model, this.flag);
            }
          })
        })
      }
    }
  }
</script>
<style>
/* 在Chrome浏览器下 */
.ratio_payment input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
/* 在Firefox浏览器下 */
.ratio_payment input[type="number"]{
    -moz-appearance: textfield;
}
</style>
